Enable SrcSet support with two different input formats for Image tag helper#46
Merged
robearlam merged 66 commits intoSitecore:mainfrom Nov 21, 2025
Merged
Conversation
…er support, Specific exception handling - Only catch JsonException instead of all exceptions, Better ASP.NET Core compatibility - Replaced HttpUtility with QueryHelpers
…e tests - Keep GetSitecoreMediaUriWithPreservation - Add 14 new test cases covering srcset parameter merging, validation, and edge cases - Fix null handling and zero/negative width filtering in ImageTagHelper - Ensure development environment compatibility with CDN/edge URLs
…ty and comprehensive test cases added Updated - Enable SrcSet support with Content SDK approach compatibility and comprehensive test cases added
There was a problem hiding this comment.
Pull Request Overview
This PR implements comprehensive responsive image support for the ImageTagHelper by adding srcset and sizes attribute functionality. The implementation enables developers to output responsive images using Sitecore's built-in media resizing capabilities while maintaining compatibility with the Content SDK behavior.
- Added
SrcSetandSizesproperties toImageTagHelperwith support for multiple input formats (anonymous objects, dictionary arrays, JSON strings) - Implemented parameter merging logic that preserves existing URL parameters while allowing srcSet parameters to override base imageParams
- Added extensive test coverage for various srcSet scenarios including parameter precedence, validation, and edge cases
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
ImageTagHelper.cs |
Core implementation of srcSet/sizes support with parameter parsing and URL generation logic |
SitecoreFieldExtensions.cs |
Added parameter preservation methods and URL building logic for responsive images |
ImageTagHelperFixture.cs |
Comprehensive test suite covering all srcSet functionality and edge cases |
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
…ng StringComparer.OrdinalIgnoreCase for the dictionary to ensure consistent parameter key handling
robearlam
requested changes
Jul 17, 2025
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
…directly creating the final mergedParams dictionary
…ethod - ApplyJssMediaUrlPrefix
…t to ParseJsonSrcSet, move the type checks to the calling method and simplifying ParseJsonSrcSet
…t to ParseJsonSrcSet, move the type checks to the calling method and simplifying ParseJsonSrcSet
…he test cases to validate the fix
…ions to handle absolute and relative URIs separately
… absolute URIs separately - Replace string manipulation with Uri class properties for absolute URIs - Add proper handling for relative URIs that throw InvalidOperationException when accessing Uri.Query - Use HttpUtility.ParseQueryString for absolute URIs and QueryHelpers.ParseQuery for relative URIs - Add explanatory comments for the if/else branching logic - Simplify ParseUrlParams method while maintaining compatibility with all URI types
sc-ivanlieckens
requested changes
Aug 18, 2025
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
…ng the explicit type declarations with target-typed new expressions
…LINQ to eliminate the early return statement
…atement in the ParseUrlParams method
…tAttribute - ImageTagHelper
…8 - Style, Readability, and Improvements - Refactored methods to avoid multiple return statements and redundant branches. - Replaced explicit type declarations with target-typed new expressions (IDE0090). - Eliminated early returns in favor of switch/case patterns for better readability. - Updated substring extraction and return statements to use C# range indexers. - Removed all var usages, replacing them with explicit types. - Removed unnecessary null checks and used collection initializer syntax for lists. - Improved overall code clarity, consistency, and compliance with project style guidelines.
sc-ivanlieckens
requested changes
Sep 5, 2025
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
- Extract ParseAbsoluteUriParams and ParseRelativeUriParams; make ParseUrlParams a single-return wrapper. - Convert ParseRelativeUriParams and GetSitecoreMediaUriWithPreservation to single-return style. - Preserve existing parsing behavior for absolute and relative URIs.
…ediaUriWithPreservation
…le - PR Review - 09/15 - Extract ParseAbsoluteUriParams and ParseRelativeUriParams; make ParseUrlParams a single-return wrapper. - Convert ParseRelativeUriParams and GetSitecoreMediaUriWithPreservation to single-return style. - Preserve existing parsing behavior for absolute and relative URIs. - Improve debuggability and reduce branching/early return
sc-ivanlieckens
approved these changes
Nov 20, 2025
robearlam
approved these changes
Nov 21, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description / Motivation
This PR implements responsive image support for the
ImageTagHelperby addingsrcsetandsizesattribute functionality, enabling developers to output responsive images using Sitecore's built-in media resizing capabilities.Key Changes:
Added
SrcSetandSizesProperties: Extended theImageTagHelperwith new properties to support responsive image configuration for both<sc-img>and<img>tags.Multiple Input Format Support: The
SrcSetproperty accepts two different input formats for maximum flexibility:new object[] { new { w = 800 }, new { mw = 400 } }new Dictionary<string, object> { {"w", 800} }Content SDK Parameter Priority: Implements the same parameter precedence as Content SDK:
w(width) takes priority overmw(max width) for srcset descriptorsw>mw>width>maxWidthgetSrcSetfunction behavior exactlyEnhanced Parameter Merging with Preservation:
imageParamsare merged with individualsrcSetparameterssrcSetparameters overrideimageParamsfor each srcset entryGetSitecoreMediaUriWithPreservationmethod to preserve existing query parameters{ ...imageParams, ...params }merge patternDual URL Processing Methods:
GetSitecoreMediaUri: Maintains existing behavior (strips query parameters)GetSitecoreMediaUriWithPreservation: Preserves critical parameters for responsive imagesGetMediaLinkForSrcSet: Specialized method for srcSet URL generation with parameter preservationContent SDK Compatibility:
The implementation now produces identical output to the Content SDK's
Imagecomponent:Content SDK (React):
ASP.NET Core SDK (Razor):
Technical Implementation Details:
Parameter Preservation Architecture:
MergeParameters(): Merges base and override parameters using reflection and dictionary handlingGetSitecoreMediaUriWithPreservation(): Preserves existing URL parameters while adding new onesConvertToStringDictionary(): Converts objects to string dictionaries for URL buildingGetMediaLinkForSrcSet(): Specialized extension method for responsive image URL generationJsonElement Handling: Enhanced support for JSON deserialization scenarios to handle
JsonElementtypes properlyHelper Methods: Added
AddResponsiveImageAttributes()overloads to eliminate code duplication acrossProcess(),GenerateImage(), and editable markup methodsUsage Examples:
This implementation ensures feature parity with the Content SDK while maintaining the familiar ASP.NET Core TagHelper syntax, full backward compatibility, and proper parameter preservation for functional image display.
Testing
Terms
Fix #16